﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using PairingMatrix.Common.Enum;
using PairingMatrix.Services;

namespace PairingMatrix.Model
{
    public class TeamMemberModel : BusinessModelBase
    {
        private readonly PairingMatrixServiceClient _dataService;

        public TeamMemberModel(Guid id, string name, PairingMatrixServiceClient dataService, ObservableCollection<DateTime> iceCreams)
        {
            _id = id;
            Name = name;
            
            IceCreams = new ObservableCollection<DateTime>();
            iceCreams.ToList().ForEach(i => IceCreams.Add(i));

            _dataService = dataService;
            PairedWith = new ObservableCollection<PairedWithModel>();
            ProjectCardAssignments = new ObservableCollection<ProjectCardAssignmentModel>();
        }

        private Guid _id;
        public Guid Id
        {
            get { return _id; }
        }

        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name == value)
                    return;
                _name = value;
                PhotoUri = _name + "jpg";
                OnPropertyChanged("Name");
            }
        }

        public ObservableCollection<DateTime> IceCreams { get; set; }

        private string _photoUri;
        public string PhotoUri
        {
            get { return _photoUri; }
            set
            {
                if (_photoUri == value)
                    return;
                _photoUri = value;
                OnPropertyChanged("PhotoUri");
            }
        }

        private Point _position;

        public Point Position
        {
            get { return _position; }
            set
            {
                if (_position == value)
                    return;
                _position = value;
                OnPropertyChanged("Position");
            }
        }

        public void SetPairingStats(List<PairModel> pairs)
        {
            var pairsWithThisMember = pairs.Where(p => p.Members.Any(m => m.Name == Name)).ToList();
            pairsWithThisMember.Sort();

            PairedWith.Clear();
            pairsWithThisMember.ForEach(p => PairedWith.Add(new PairedWithModel(p, Name)));

            OnPropertyChanged("NumberOfSignUpsWithSelectedTeamMember");
        }

        public ObservableCollection<PairedWithModel> PairedWith { get; set; }
        public ObservableCollection<ProjectCardAssignmentModel> ProjectCardAssignments { get; set; }

        public void Save()
        {
            _dataService.UpdateTeamMemberDetailsAsync(_id, _name, _photoUri);
        }

        private WallType _wall = Registry.CurrentWall;
        public WallType Wall
        {
            get { return _wall; }
            set
            {
                if (_wall == value)
                    return;
                _wall = value;
                OnPropertyChanged("Wall");
                OnPropertyChanged("HasSignUpsOnCurrentWall");
            }
        }


        public bool HasSignUpsOnCurrentWall
        {
            get { return _wall == Registry.CurrentWall; }
        }

        public int NumberOfSignUpsWithSelectedTeamMember 
        {
            get
            {
                var seletedTeamMemberId = Registry.SeletedTeamMemberId;
                var pairedWithModel = PairedWith.FirstOrDefault(p => p.Member.Id == seletedTeamMemberId);
                if (pairedWithModel == null)
                    return 0;
                return pairedWithModel.NumberOfTimesPaired;
            }
        }

        public PairingLevel PairingLevelWithSelectedTeamMember
        {
            get
            {
                var numberOfSignUps = NumberOfSignUpsWithSelectedTeamMember;
                if (numberOfSignUps >= 0 && numberOfSignUps <= 1)
                    return PairingLevel.ShouldDefinitelyPair;
                if (numberOfSignUps == 2)
                    return PairingLevel.TryToPairMore;
                if (numberOfSignUps == 3)
                    return PairingLevel.CanPair;
                if (numberOfSignUps > 3 && numberOfSignUps <= 5)
                    return PairingLevel.AvoidPairing;
                return PairingLevel.ShouldDefinitelyNotPair;
            }

        }

        public void SetProcessAssignments(List<ProjectCardAssignmentModel> projectCardAssignmentModels)
        {
            ProjectCardAssignments.Clear();
            projectCardAssignmentModels.ForEach(p => ProjectCardAssignments.Add(p));
        }

        private bool _showCardInAbsenteesBox;
        public bool ShowCardInAbsenteesBox
        {
            get { return _showCardInAbsenteesBox; }
            set
            {
                if (_showCardInAbsenteesBox == value)
                    return;
                _showCardInAbsenteesBox = value;
                OnPropertyChanged("ShowCardInAbsenteesBox");
            }
        }

        public void AddIceCream(DateTime date)
        {
            IceCreams.Add(date);
            _dataService.AddIceCreamToTeamMemberAsync(Id, date);
        }

        public void ClearIceCreams()
        {
            IceCreams.Clear();
            _dataService.ClearIceCreamsForTeamMemberAsync(Id);
        }
    }
}